package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cleveroad.fanlayoutmanager.FanLayoutManager;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.contentshow.Details;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.databinding.RecommendationShowItemBinding;

import java.util.List;

import static android.view.View.INVISIBLE;

/**
 * @author puyv
 * @createTime 2021/11/6 20:46
 * @projectName CoffeeLoverSupportLibrary
 * @className RecommendationListAdapter.java
 * @description TODO
 */
public class RecommendationListAdapter extends RecyclerView.Adapter {
    private List<Article> articles;
    private FanLayoutManager fanLayoutManager;
    private RecyclerView recyclerView;

    public RecommendationListAdapter(List<Article> articles,FanLayoutManager fanLayoutManager,RecyclerView recyclerView) {
        this.articles = articles;
        this.fanLayoutManager=fanLayoutManager;
        this.recyclerView=recyclerView;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommendation_show_item,parent,false);
        RecommendationShowItemBinding recommendationShowItemBinding=RecommendationShowItemBinding.bind(view);
        return new RecommendationArticleViewHolder(recommendationShowItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Article article=articles.get(position);

        Glide.with(((RecommendationArticleViewHolder)holder)
                .recommendationShowItemBinding
                .getRoot().getContext())
                .load(article.getArticleBanner())
                .into(((RecommendationArticleViewHolder)holder)
                .recommendationShowItemBinding
                .recommendationItemImage);


        ((RecommendationArticleViewHolder)holder)
                .recommendationShowItemBinding.recommendationItemContent.setText(article.getArticleTitle());


        ((RecommendationArticleViewHolder)holder)
                .recommendationShowItemBinding
                .getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fanLayoutManager.switchItem(recyclerView,position);
            }
        });

        ((RecommendationArticleViewHolder)holder)
                .recommendationShowItemBinding
                .enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(holder.itemView.getContext(), Details.class);
                intent.putExtra("articleId",article.getArticleId());
                holder.itemView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

    static class RecommendationArticleViewHolder extends RecyclerView.ViewHolder{
        RecommendationShowItemBinding recommendationShowItemBinding;
        public RecommendationArticleViewHolder(@NonNull RecommendationShowItemBinding recommendationShowItemBinding) {
            super(recommendationShowItemBinding.getRoot());
            this.recommendationShowItemBinding=recommendationShowItemBinding;
        }
    }
}
